package com.sjd.farm.service.impl;

import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.utils.SecurityUtils;
import com.sjd.farm.domain.SjdFarmOrderDO;
import com.sjd.farm.domain.dto.order.WebFarmOrderQryRequestDTO;
import com.sjd.farm.domain.dto.order.WebFarmOrderQryResponseDTO;
import com.sjd.farm.mapper.SjdFarmOrderDao;
import com.sjd.farm.mapper.SjdFarmOrderMapper;
import com.sjd.farm.service.WebFarmOrderService;
import com.sjd.mall.domain.dto.base.OrderNoRequestDTO;
import com.sjd.mall.enums.OrderStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

/**
 * WebFarmOrderServiceImpl
 *
 * @author seer
 * @version 1.0
 * @date 2022/2/5 13:32
 */
@Service
@Slf4j
public class WebFarmOrderServiceImpl implements WebFarmOrderService {

    @Autowired
    private SjdFarmOrderMapper farmOrderMapper;

    @Autowired
    private SjdFarmOrderDao farmOrderDao;

    /**
     * 确认收货
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @Override
    public AjaxResult receipt(OrderNoRequestDTO requestDTO) {
        SjdFarmOrderDO orderDO = farmOrderMapper.getByOrderNo(requestDTO.getOrderNo());
        Assert.notNull(orderDO, "订单不存在 " + requestDTO.getOrderNo());
        if (!OrderStatus.PAY_SUCCESS.value.equals(orderDO.getOrderStatus())) {
            return AjaxResult.error("订单状态错误，不允许确认收货");
        }
        SjdFarmOrderDO update = new SjdFarmOrderDO();
        update.setOrderStatus(OrderStatus.RECEIPT_SUCCESS.value);
        update.setOrderStatusTime(LocalDateTime.now());
        update.setReceiptOperatorId(SecurityUtils.getUserId());
        update.setReceiptTime(LocalDateTime.now());
        update.setOrderNo(requestDTO.getOrderNo());
        farmOrderMapper.updateSelectiveByOrderNo(update);
        return AjaxResult.success();
    }

    /**
     * 查询订单
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @Override
    public List<WebFarmOrderQryResponseDTO> qry(WebFarmOrderQryRequestDTO requestDTO) {
        return farmOrderDao.listOrder(requestDTO);
    }
}
